package com.mapapp.flutter_mapapp.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import java.util.List;

public class BarChart extends View {
    public static final String TAG ="BarChart";
    private int mViewWidth;
    private int mViewHight;
    private List<Double> mRecords;
    private Paint mTextPaint;
    private Paint mRectPaint1,mRectPaint2,mRectPaint3,mRectPaint4;

    public BarChart(Context context) {
        super(context);
        init();
    }

    public BarChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(Color.BLACK);        // 设置颜色
        mTextPaint.setStyle(Paint.Style.FILL);   // 设置样式
        mTextPaint.setTextSize(20);              // 设置字体大小
        mRectPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRectPaint1.setStyle(Paint.Style.FILL);
        mRectPaint1.setColor(Color.parseColor("#ea698d"));
        mRectPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRectPaint2.setStyle(Paint.Style.FILL);
        mRectPaint2.setColor(Color.parseColor("#F2AC18"));
        mRectPaint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRectPaint3.setStyle(Paint.Style.FILL);
        mRectPaint3.setColor(Color.parseColor("#fbca07"));
        mRectPaint4 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRectPaint4.setStyle(Paint.Style.FILL);
        mRectPaint4.setColor(Color.parseColor("#00ff00"));
        Log.i(TAG,"BarChart init");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mViewWidth = getMeasuredWidth();
        mViewWidth = getMeasuredHeight();
        Log.d(TAG, "onMeasure: width----" + mViewWidth);
        Log.d(TAG, "onMeasure: height----" + mViewWidth);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewWidth = w;
        mViewHight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mRecords == null){
            return;
        }
        for(int i = 0 ; i < mRecords.size() ; i ++){
            int left = 0;
            int top = i*(mViewHight/4);
            int right = (int)Math.ceil((mRecords.get(i)/100)*mViewWidth);
            int bottom = (i+1)*(mViewHight/4);
            Rect rect = new Rect(left,top,right,bottom);
            String str = mRecords.get(i)+"%";
            if(0 == i){
                canvas.drawRect(rect,mRectPaint1);
            }else if(1 == i) {
                canvas.drawRect(rect,mRectPaint2);
            }else if(2 == i) {
                canvas.drawRect(rect,mRectPaint3);
            }else if(3 == i) {
                canvas.drawRect(rect,mRectPaint4);
            }
            canvas.drawText(str,0,str.length(),0,(i+1)*(mViewHight/4),mTextPaint);
        }
        Log.i(TAG, "BarChart onDraw");
    }

    public void setData(List<Double> records) {
        this.mRecords = records;
        invalidate();   // 刷新
    }
}
